package com.smartHome.controller;

import com.smartHome.pojo.vo.EnergyConsumptionVO;
import com.smartHome.service.EnergyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/energy")
public class EnergyController {

    @Autowired
    private EnergyService energyService;

    @GetMapping("/house/consumption")
    public Map<String, Object> getHouseConsumption(
            @RequestParam Integer houseId,
            @RequestParam Integer timeRange) {
        try {
            EnergyConsumptionVO data = energyService.getHouseConsumption(houseId, timeRange);
            return Map.of(
                    "code", 200,
                    "message", "success",
                    "data", data);
        } catch (Exception e) {
            return Map.of(
                    "code", 500,
                    "message", e.getMessage());
        }
    }

    @GetMapping("/room/consumption")
    public Map<String, Object> getRoomConsumption(
            @RequestParam Integer roomId,
            @RequestParam Integer timeRange) {
        try {
            EnergyConsumptionVO data = energyService.getRoomConsumption(roomId, timeRange);
            return Map.of(
                    "code", 200,
                    "message", "success",
                    "data", data);
        } catch (Exception e) {
            return Map.of(
                    "code", 500,
                    "message", e.getMessage());
        }
    }

    @GetMapping("/device/consumption")
    public Map<String, Object> getDeviceConsumption(
            @RequestParam Integer deviceId,
            @RequestParam Integer timeRange) {
        try {
            EnergyConsumptionVO data = energyService.getDeviceConsumption(deviceId, timeRange);
            return Map.of(
                    "code", 200,
                    "message", "success",
                    "data", data);
        } catch (Exception e) {
            return Map.of(
                    "code", 500,
                    "message", e.getMessage());
        }
    }

    @GetMapping("/suggestions")
    public Map<String, Object> getEnergySuggestions(@RequestParam Integer houseId) {
        try {
            List<String> suggestions = energyService.getEnergySuggestions(houseId);
            return Map.of(
                    "code", 200,
                    "message", "success",
                    "data", suggestions);
        } catch (Exception e) {
            return Map.of(
                    "code", 500,
                    "message", e.getMessage());
        }
    }
}